#include "tensor.h"
#include <string.h>
#include <math.h>

double CalcuP(Stress sigma)
{
    return (sigma.x + sigma.y + sigma.z) / 3.0;
}

void CalcuDeviatoric(Stress sigma, Stress Deviatoric)
{
    double p = CalcuP(sigma);
    memset(&Deviatoric, 0, sizeof(Stress));
    Deviatoric.x = sigma.x - p;
    Deviatoric.y = sigma.y - p;
    Deviatoric.z = sigma.z - p;
    Deviatoric.xy = sigma.xy;
    Deviatoric.yz = sigma.yz;
    Deviatoric.zx = sigma.zx;
}

double J2(Stress sigma)
{
    double temp = 0;
    Stress Deviatoric;
    CalcuDeviatoric(sigma, Deviatoric);
    return 0.5 * (Deviatoric.x * Deviatoric.x +
                  Deviatoric.y * Deviatoric.y +
                  Deviatoric.z * Deviatoric.z) +
           Deviatoric.xy * Deviatoric.xy +
           Deviatoric.yz * Deviatoric.yz +
           Deviatoric.zx * Deviatoric.zx;
}

double CalcuQ(Stress sigma)
{
    return sqrt(3.0 * J2(sigma));
}